using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

using PHSRAG.Utility;


namespace PHSRAG.WebServices
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://www.partners.org/PHSRAG-WebServices/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Agreements : System.Web.Services.WebService
    {
        private const string VERIFY_FUND_NUM = "dbo.usp_VerifyFundNumber";
        private enum FUND_NUM_STATUS
        {
            ERROR = -1,
            ACTIVE = 1,
            INACTIVE = 2,
            INVALID = 3
        }

        [WebMethod]
        public OperationalResult VerifyFundNumber(int fundNumber)
        {
            OperationalResult ors = new OperationalResult();

            try
            {
                //strip out the leading digit from the passed in value (ROE prefixes their fund ID's with a "9")
                string fundNum = fundNumber.ToString().Substring(1);
                
                SqlDataReader rdr = Utility.DataAccess.ExecuteReader(Utility.DataAccess.ConnectionString, VERIFY_FUND_NUM,
                        Utility.DataAccess.BuildSqlParameterArray("@fund_num", SqlDbType.NVarChar, fundNum));

                if (rdr.Read())
                {
                    ors.ReturnCode = int.Parse(rdr["FundStatusTypeId"].ToString());
                    switch (ors.ReturnCode)
                    {
                        case (int)FUND_NUM_STATUS.ACTIVE:
                            ors.Description = "Active Fund";
                            break;
                        case (int)FUND_NUM_STATUS.INACTIVE:
                            ors.Description = "Inactive Fund";
                            break;
                        case (int)FUND_NUM_STATUS.INVALID:
                            ors.Description = "Not a valid fund number";
                            break;
                        case (int)FUND_NUM_STATUS.ERROR:
                            ors.Description = "Error encountered";
                            break;
                        default:
                            ors.Description = "Unexpected result";
                            break;
                    }
                }
                else
                {
                    ors.ReturnCode = (int)FUND_NUM_STATUS.INVALID;
                    ors.Description = "Invalid Fund Number";
                }
            }
            catch (Exception ex)
            {
                ors.ReturnCode = -1;
                ors.Description = ex.Message;
            }
            
            return ors;
        }
    }
}
